var shopcartEventAlpha = function() {};
$(function() {
	var html = [];
	var _shopcartinitClass = new shopcartInit();
	shopcartEventAlpha.prototype = {
		//跳转到商品详情
		jumpToGoodsDetail: function() {
			$("#main").on("click", "div[name='goodsItem']", function() {
				var title = $("#editCart").find("span:first-child").html();
				if ("完成" == title) {
					return;
				}
				var id = $(this).attr('tip');
				redirect("../goods/goodsdetail.html?id=" + id);
			});
		},
		//选择优惠活动
		salesPromotion: function(goodsData, user) {
			//优惠活动
			$("#main").on("click", "span[name=sales_promotion]", function() {
				var tips = JSON.parse($(this).attr('tips'));
				switch (tips.promotion.type) {
					//优惠券
					case "coupons":
						var coupons = goodsData.supplier[tips.supplierIndex].coupons;
						if (coupons) {
							html = [];
							$.each(coupons, function(i, c) {
								html.push('<div name="item_coupon" class="shp-gwcny_center">');
								html.push('<p class="shp-ms shp-ms1">优惠券</p>');
								html.push('<p class="shp-coudan">');
								var title = (c.title.length > 14) ? (c.title.substr(0, 14) + '...') : c.title;
								var promoTips = {
									type: "coupon",
									action: "add",
									url: "v1/member/coupon/gain/" + c.coupon_no + "-" + user.id,									
									tips:tips,
									param: c,
								};
								html.push(title);
								html.push('</p>');
								html.push('<p name="promotionaction" tip=' + JSON.stringify(promoTips) + ' class="ob-cha-select"><a style="width:58px;"  class="receive_button">立即领取</a></p>')
								html.push('</div>');
							});
							//取消使用
//							html.push('<div class="shp-gwcny_center">');
//							var promoTips = {
//								type: "coupon",
//								action: "remove",
//								url: "v1/cart/fg/" + CONFIG_MANAGER.orgId + "/" + user.id + "/" + getSessionID() + "/" + gifts[0].sku_id + "/" + shopId + "/0",
//								param: tips
//							};
//							html.push('<p name="promotionaction" tip=' + JSON.stringify(promoTips) + '  class="ob-cha-select">');
//							html.push('<a style="width:58px;border:1px solid #c6c6c6;color:#c6c6c6" class="receive_button">取消使用</a>');
//							html.push('</p>');							
//							html.push('</div>');							
						}
						$("#active_title").html("优惠券");
						break;
						//满赠
					case "full_gift":
						var gifts = goodsData.supplier[tips.supplierIndex].full_gift;
						var shopId = goodsData.supplier[tips.supplierIndex].shop_id;
						if (gifts && gifts.length > 0) {
							html = [];
							$.each(gifts, function(i, gift) {
								html.push('<div class="shp-cart-item-core shp_gwcny_suit giftitem_margin_top">');
								html.push('<a class="cart-product-cell-1" href="javascript:void(0);">');
								html.push('<img class="cart-photo-thumb" src="' + getImageUrl(gift.logo) + '">');
								html.push('</a>');
								html.push('<div class="cart-product-cell-2">');
								html.push('<div class="cart-product-name1">');
								html.push('<a href="javascript:void(0);"><span>' + gift.name + '</span></a>');
								var promoTips = {
									type: "full_gift",
									action: "add",
									url: "v1/cart/fg/" + CONFIG_MANAGER.orgId + "/" + user.id + "/" + getSessionID() + "/" + gift.sku_id + "/" + shopId + "/" + gift.promo_id,
									tips: tips,
									param: gift
								};
								html.push('<p name="promotionaction" tip=' + JSON.stringify(promoTips) + ' class="ob-cha-select" ><a style="width:58px;" class="receive_button">选取</a></p>');
								html.push('</div>');
								html.push('<div class="cart-product-name w40"><a href="javascript:void(0);"><span>' + gift.spec + '</span></a></div>');
								html.push('<div class="shp-cart-opt">');
								html.push('</div>');
								html.push('</div>');
								html.push('</div>');
							});
							//取消使用
							html.push('<div class="shp-gwcny_center">');
							var promoTips = {
								type: "full_subtract",
								action: "remove",
								url: "v1/cart/fg/" + CONFIG_MANAGER.orgId + "/" + user.id + "/" + getSessionID() + "/" + gifts[0].sku_id + "/" + shopId + "/0",
								param: tips
							};
							html.push('<p name="promotionaction" tip=' + JSON.stringify(promoTips) + '  class="ob-cha-select">');
							html.push('<a style="width:58px;border:1px solid #c6c6c6;color:#c6c6c6" class="receive_button">取消使用</a>');
							html.push('</p>');							
							html.push('</div>');
							
							$("#active_title").html("满赠");
						}
						break;
						//满减
					case "full_subtract":
						var subtracts = goodsData.supplier[tips.supplierIndex].full_subtract;
						var shopId = goodsData.supplier[tips.supplierIndex].shop_id;
						if (subtracts && subtracts.length > 0) {
							html = [];
							$.each(subtracts, function(i, subtract) {
								html.push('<div name="item_coupon" class="shp-gwcny_center">');
								html.push('<p class="shp-ms shp-ms1">满减</p>');
								html.push('<p class="shp-coudan">');
								html.push(subtract.promo_title);
								html.push('</p>');
								subtract.promo_title = subtract.promo_title.Trim('g');
								var promoTips = {
									type: 'full_subtract',
									action: "add",
									url: 'v1/cart/fs/' + CONFIG_MANAGER.orgId + '/' + user.id + '/' + getSessionID() + '/' + shopId + '/' + subtract.promo_id,
									tips: tips,
									param: subtract,
								};
								html.push('<p name="promotionaction" tip=' + JSON.stringify(promoTips) + ' class="ob-cha-select"><a style="width:58px;"  class="receive_button">选择</a></p>');
								html.push('</div>');
							});
							//取消使用
							html.push('<div class="shp-gwcny_center">');
							var promoTips = {
								type: "full_subtract",
								action: "remove",
								url: "v1/cart/fs/" + CONFIG_MANAGER.orgId + "/" + user.id + "/" + getSessionID() + "/" + shopId + "/0",
								tips: tips,
							};
							//取消活动
							html.push('<p name="promotionaction" tip=' + JSON.stringify(promoTips) + ' class="ob-cha-select">');
							html.push('<a style="width:58px;border:1px solid #c6c6c6;color:#c6c6c6" class="receive_button">取消使用</a>');
							html.push('</p>');
							
							html.push('</div>');
							
							$("#active_item").html(html.join(''));
							$("#active_title").html("满减");
						}
						break;
					case "over_bought":

						break;
					case "suite":

						break;
						//商品优惠活动
					case "promos":
						var promos = goodsData.supplier[tips.supplierIndex].goods[tips.goodsIndex].promos;
						var targetId = goodsData.supplier[tips.supplierIndex].goods[tips.goodsIndex].sku_id
						if (promos && promos.length > 0) {
							html = [];
							$.each(promos, function(i, promo) {
								html.push('<div name="item_coupon" class="shp-gwcny_center">');
								html.push('<p class="shp-ms shp-ms1">促销</p>');
								html.push('<p class="shp-coudan">');
								html.push(promo.promo_title);
								html.push('</p>');
								promo.promo_title = promo.promo_title.Trim('g');
								var promoTips = {
									type: "promos",
									action: "add",
									url: "v1/cart/promo/"+ CONFIG_MANAGER.orgId +"/"+ user.id +"/"+ targetId +"/" + promo.promo_id,
									tips: tips,
									param: promo
								};
								html.push('<p name="promotionaction" tip=' + JSON.stringify(promoTips) + ' class="ob-cha-select"><a style="width:58px;"  class="receive_button">选择</a></p>')
								html.push('</div>');
							});
							//取消使用
							html.push('<div class="shp-gwcny_center">');
							var promoTips = {
								type: "promos",
								action: "remove",
								url: "v1/cart/promo/"+ CONFIG_MANAGER.orgId +"/"+ user.id +"/"+ targetId +"/0",
								tips: tips,
							};
							//取消活动
							html.push('<p name="promotionaction" tip=' + JSON.stringify(promoTips) + ' class="ob-cha-select">');
							html.push('<a style="width:58px;border:1px solid #c6c6c6;color:#c6c6c6" class="receive_button">取消使用</a>');
							html.push('</p>');
							html.push('</div>');
							$("#active_item").html(html.join(''));
							$("#active_title").html("优惠活动");
						}
						break;
					default:
						break;
				}
				$("#active_item").html(html.join(''));
				$("#givegoodsback").show();
				$("#givegoodspop").show();
				setTimeout(function() {
					$("#givegoodspop").addClass("moveFromBottom-moved");
				}, 200);
			});

			//领取某个优惠活动&&优惠券
			$("#givegoodspop").on("click", "#active_item p[name=promotionaction]", function() {
				$("#givegoodspop").removeClass("moveFromBottom-moved");
				setTimeout(function() {
					$("#givegoodspop").hide();
					$("#givegoodsback").hide();
				}, 200);
				
				var that = this;
				var tips = JSON.parse($(this).attr("tip"));
				var requestUrl = CONFIG_MANAGER.RequstUrl + tips.url;
				$("#shop_"+tips.tips.supplierIndex + '>div.shp-gwcny').remove();
				if('add' == tips.action){
					var html = [];
					html.push('<div class="shp-gwcny">');
					html.push('<p class="shp-ms-icon shp-ms1">'+ tips.param.promo_name +'</p>');
					html.push('<p class="shp-coudan">' + tips.param.promo_title + '</p>');
					html.push('</div>');
					$("#shop_"+tips.tips.supplierIndex).append(html.join(''));
				}
				$.ajax({
					type: "GET",
					url: requestUrl,
					async: true,
					headers: {
						token: user.token
					},
					beforeSend: function() { }
				}).done(function(data) {
//					_shoppingcartPage = new shoppingcartPageReady();
					switch (tips.type) {
						case "coupon":
							$.toast("您已领取优惠券 ");
							break;
						case "full_gift":
							$.toast("您已选择满增 ");
							break;
						case "full_subtract":							
							$("#total").html(data.total_money);
							$("#totalCoin").html(data.total_coin);
							$.toast("您已选择满减 ");
							break;
						case "promos":
							$.toast("您已变更活动 ");
							break;
						default:
							break;
					}
					//TODO
//					if(AGENT_CLIENT.APP){
//						plus.webview.getWebviewById("../shopcart/shoppingcart.html").evalJS("reloadPage()");
//					}else{
//						redirect("../shopcart/shoppingcart.html");
//					}
					
					$("#givegoodspop").removeClass("moveFromBottom-moved");
					setTimeout(function() {
						$("#givegoodspop").hide();
						$("#givegoodsback").hide();
					}, 200);
				}).fail(function(data) {
					$.toast(data.responseJSON.Message);
				}).always(function() {
					$.closeWaiting();
				});
			});

			//叉号取消选择
			$("#givegoodspop").on("click", "#cancelgivegoods", function() {
				$("#givegoodspop").removeClass("moveFromBottom-moved");
				setTimeout(function() {
					$("#givegoodspop").hide();
					$("#givegoodsback").hide();
				}, 200);
			});
			//蒙板取消选择
			$("#givegoodsback").on("click", function() {
				$("#givegoodspop").removeClass("moveFromBottom-moved");
				setTimeout(function() {
					$("#givegoodspop").hide();
					$("#givegoodsback").hide();
				}, 200);
			});
			//删除选择
			$(".main").on("click", "div.items div[name=remove_give]", function() {
				$(this).parent().remove();
			});
		},
	};
});